What is centra?
Centra is a lightweight HTTP client for Node.js that allows you to make HTTP requests with a simple and intuitive API. It supports various HTTP methods, setting headers, sending data, and handling responses.
What are centra's main functionalities?
Basic GET Request
This feature allows you to make a basic GET request to a specified URL and handle the response. In this example, it fetches a post from a placeholder API and logs the JSON response.
const centra = require('centra');
async function fetchData() {
const res = await centra('https://jsonplaceholder.typicode.com/posts/1').send();
console.log(await res.json());
}
fetchData();
POST Request with JSON Body
This feature allows you to make a POST request with a JSON body. The example sends a new post to the placeholder API and logs the response.
const centra = require('centra');
async function postData() {
const res = await centra('https://jsonplaceholder.typicode.com/posts', 'POST')
.body({ title: 'foo', body: 'bar', userId: 1 }, 'json')
.send();
console.log(await res.json());
}
postData();
Setting Headers
This feature allows you to set custom headers for your HTTP requests. The example demonstrates setting an Authorization header before making a GET request.
const centra = require('centra');
async function fetchDataWithHeaders() {
const res = await centra('https://jsonplaceholder.typicode.com/posts/1')
.header('Authorization', 'Bearer token')
.send();
console.log(await res.json());
}
fetchDataWithHeaders();
Handling Different Response Types
This feature allows you to handle different response types such as text, JSON, or buffers. The example fetches a post and logs the response as plain text.
const centra = require('centra');
async function fetchData() {
const res = await centra('https://jsonplaceholder.typicode.com/posts/1').send();
const text = await res.text();
console.log(text);
}
fetchData();
Other packages similar to centra
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a more feature-rich API compared to Centra, including request and response interceptors, automatic JSON data transformation, and support for older browsers.
node-fetch
Node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is similar to Centra in terms of simplicity and ease of use but is designed to be a minimal implementation of the Fetch API.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It offers a more extensive feature set than Centra, including retry mechanisms, advanced error handling, and support for streams.
The core lightweight HTTP client for Node
GitHub | NPM
Install
npm i centra
Why centra?
centra is the best request library for developers; it provides a number of extremely useful features while still being one of the most lightweight Node.js HTTP client libraries available.
Use centra!
First, require the library.
const c = require('centra')
Then let's make a request in an async function!
;(async () => {
const res = await c('https://example.com').send()
console.log(await res.text())
})()
More advanced usage
Send data in a JSON body
c('https://example.com/nonexistentJSONAPI', 'POST').body({
'name': 'Ethan'
}, 'json').send().then((res) => {
})
Send data in a form body
c('https://example.com/nonexistentJSONAPI', 'POST').body({
'name': 'Ethan'
}, 'form').send().then((res) => {
})
Set query string parameters
One at a time:
c('https://example.com/user').query('id', 'u1817760').send().then((res) => {
})
Many at a time:
c('https://example.com/user').query({
'id', 'u1817760',
'name': 'Ethan'
}).send().then((res) => {
})
Set a request timeout
c('https://example.com').timeout(2000).send().then((res) => {
}).catch((err) => {
})
Stream a request's response
In this example, the stream is piped to a file:
c('https://example.com').stream().send().then((stream) => stream.pipe(fs.createWriteStream(path.join(__dirname, 'logo.png'))))
Follow redirects
c('https://example.com/').followRedirects(5).send()
Switch paths on the fly
c('https://example.com/test').path('/hello').send()
One at a time:
c('https://example.com').header('Content-Type', 'application/json').send()
Many at a time:
c('https://example.com').header({
'Content-Type': 'application/json',
'X-Connecting-With': 'centra'
}).send()
Modify core HTTP request options
See http.request's options for more information about core HTTP request options.
Let's change our localAddress as an example.
c('https://example.com').option('localAddress', '127.0.0.2').send()
Accept compressed responses
c('https://example.com').compress().send()